home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / s342q12.lha / sysarc.c < prev    next >
C/C++ Source or Header  |  1996-03-21  |  11KB  |  425 lines

  1. /*
  2. *       sysarc.c
  3. *
  4. * This file contains the system dependent code for deARCing files for
  5. * download.
  6. */
  7. /*
  8. *       history
  9. *
  10. * 91Oct07 HAW Netcache stuff.
  11. * 88Jun08 HAW Created.
  12. */
  13. #define SYSTEM_DEPENDENT
  14. #include "ctdl.h"
  15. #include "dos.h"
  16. /*#include "sys\stat.h" */
  17. /* #include "process.h" */
  18. /*
  19.  
  20. *       Contents
  21. *
  22. * ArcInit()   initialize for deARCing
  23. * CompAvailable()   is compression available for this type?
  24. * SendArcFiles()    get arc name, send contents specified
  25. * # MakeTempDir()   Create and move into a temporary dir
  26. *
  27. *   # == local for this implementation only
  28. */
  29.  
  30. extern char logNetResults;
  31. extern char netDebug;
  32. extern FILE  *netLog;
  33.  
  34. extern aRoom roomBuf;
  35. extern MessageBuffer msgBuf;
  36. extern CONFIG  cfg;
  37. extern char whichIO;
  38. char TDirBuffer[120];
  39. char cmdbuf[120];  /* command line buffer */
  40. void KillDeCompress(void);
  41.  
  42.  
  43. /*
  44. * This table must be kept in sync with Formats[] in Misc.c
  45. */
  46. DeCompElement DeComp[] =
  47.   {
  48.     { "delha.sys", "L. Lha",   NULL, NULL, NULL },
  49.     { "delzh.sys", "P. Lzh",   NULL, NULL, NULL },
  50.     { "dezoo.sys", "Z. Zoo",   NULL, NULL, NULL },
  51.     { "dearc.sys", "A. Arc",   NULL, NULL, NULL },
  52.  
  53.   };
  54.  
  55. int Jsystem(char *);
  56. static void CheckCompressedData(char *name, int index);
  57. static void MakeTempName(void);
  58. /*
  59. * ArcInit()
  60. *
  61. * This initializes this module for handling the various variants of
  62. * compression/decompression.
  63. */
  64. void ArcInit()
  65.   {
  66.   SYS_FILE name;
  67.   int i;
  68.   for (i = 0; i < NumElems(DeComp); i++)
  69.       {
  70.     makeSysName(name, DeComp[i].FileName, &cfg.roomArea);
  71.     CheckCompressedData(name, i);
  72.  
  73.     }
  74.  
  75.   }
  76. /*
  77. * CheckCompressedData()
  78. *
  79. * This function does an initial check for existence of a configuration file.
  80. */
  81. static void CheckCompressedData(char *name, int index)
  82.   {
  83.   FILE *fd;
  84.   char work[80];
  85.   if (cfg.BoolFlags.debug) splitF(NULL,"Open Attemp:%s\n",name);
  86.   if ((fd = fopen(name, "r")) == NULL)
  87.       {
  88.     return;
  89.  
  90.     }
  91.   if (cfg.BoolFlags.debug) splitF(NULL,"Opened:%s\n",name);
  92.   if (GetAString(work, sizeof work, fd) != NULL)
  93.       {
  94.       if (cfg.BoolFlags.debug) splitF(NULL,"Read 1:%s\n",work);
  95.       if (strlen(work) != 0)DeComp[index].DeWork = strdup(work);
  96.       if (GetAString(work, sizeof work, fd) != NULL)
  97.         {
  98.         if (cfg.BoolFlags.debug) splitF(NULL,"Read 2:%s\n",work);
  99.         if (strlen(work) != 0) DeComp[index].IntWork = strdup(work);
  100.         if (GetAString(work, sizeof work, fd) != NULL)
  101.           {
  102.           if (cfg.BoolFlags.debug) splitF(NULL,"Read 3:%s\n",work);
  103.           if (strlen(work) != 0) DeComp[index].CompWork = strdup(work);
  104.           };
  105.         };
  106.  
  107.       };
  108.   fclose(fd);
  109.  
  110.   }
  111. /*
  112. * SendArcFiles()
  113. *
  114. * This function gets arc filename, send specified contents.
  115. */
  116. void SendArcFiles(int protocol)
  117.   {
  118.   SYS_FILE name;
  119.   char     *temp;
  120.   int  NumFiles, format;
  121.   extern long netBytes;
  122.   getNormStr("ARCHFN", name, SIZE_SYS_FILE, 0);
  123.   if (strLen(name) == 0) return;
  124.   if (!setSpace(&roomBuf)) return;
  125.   temp = getcwd(NULL, 100);
  126.   sPrintf(msgBuf.mbtext, "%s/%s", temp, name);
  127.   homeSpace();
  128.   if (access(msgBuf.mbtext, 4) == -1)
  129.     {
  130.     for (NumFiles = 0; NumFiles < NumElems(DeComp); NumFiles++)
  131.       {
  132.       sPrintf(msgBuf.mbtext, "%s/%s.%s", temp,name, Formats[NumFiles].Format);
  133.       if (access(msgBuf.mbtext, 4) == 0) break;
  134.  
  135.       };
  136.  
  137.     };
  138.   if( access(msgBuf.mbtext,4) == -1 )
  139.     {
  140.     mPrintf("%s does not exist.\n ", name);
  141.     free(temp);
  142.     return;
  143.  
  144.     };
  145.   if ((format = CompressType(msgBuf.mbtext)) == -1)
  146.   format = 0;
  147.   if (DeComp[format].DeWork == NULL)
  148.       {
  149.     mPrintf("Sorry, de%sing not supported here.\n ",
  150.     Formats[format].Format);
  151.     return;
  152.  
  153.     }
  154.   sPrintf(msgBuf.mbtext, "%s %s/%s ",
  155.   DeComp[format].DeWork, temp, name);
  156.   getNormStr("ARCHMS", msgBuf.mbtext, 100, 0);
  157.   if (strLen(msgBuf.mbtext) == 0) strCpy(msgBuf.mbtext, ALL_FILES);
  158.   MakeTempDir();      /* Create and drop into */
  159.   mPrintf("One moment, please ...\n ");
  160.   sprintf(cmdbuf, "%s %s/%s %s",
  161.   DeComp[format].DeWork, temp, name, msgBuf.mbtext);
  162.   Jsystem(cmdbuf);
  163.   free(temp);         /* Don't need this any longer, so kill it now */
  164.   netBytes = 0l;
  165.   NumFiles = wildCard(getSize, ALL_FILES, FALSE, "", FALSE);
  166.   if (NumFiles <= 0)
  167.       {
  168.     mPrintf("Sorry, no files matched that deARC command.\n ");
  169.  
  170.     }
  171.   else
  172.       {
  173.     if ( getYesNo("MREFFI") )
  174.     if (TranAdmin(protocol, NumFiles))
  175.     TranSend(protocol, transmitFile, ALL_FILES, "", FALSE);
  176.     }
  177.   KillDeCompress();  /* clean up the temp directory */
  178.   }
  179.  
  180. /*
  181.  * KillDeCompress()
  182.  *
  183.  * This function clears out the files from the decompression.
  184.  * We assume we're IN the dir to be cleared.
  185.  */
  186. void KillDeCompress()
  187.   {
  188.   char temp[120];
  189.   homeSpace();
  190.   chdir(TDirBuffer);
  191.   getcwd(temp, 120);
  192.   if( strcmp(temp,TDirBuffer) == 0 )   /* make sure we are where we want to delete files */
  193.     {
  194.     if (!cfg.BoolFlags.debug) wildCard(DelFile, ALL_FILES, FALSE, "", FALSE);
  195.     }
  196.   else
  197.     {
  198.     splitF(netLog,"Error:  Could not set directory and delete temp dir/files\n");
  199.     splitF(netLog,"Temp dir:%s\n",TDirBuffer);
  200.     splitF(netLog,"Curr dir:%s\n",temp);
  201.     }
  202.   homeSpace();
  203.   if (!cfg.BoolFlags.debug) rmdir(TDirBuffer);
  204.   }
  205. /*
  206.  * KillNetDeCompress()
  207.  *
  208.  * This function clears out the files from the net session decompression.
  209.  *
  210.  */
  211. void KillNetDeCompress()
  212.   {
  213.   char temp[120], temp2[120];
  214.   homeSpace();
  215.   MakeNetCache(temp2);
  216.   chdir(temp2);
  217.   getcwd(temp, 120);
  218.   if( strcmp(temp,temp2) == 0 )   /* make sure we are where we want to delete files */
  219.     {
  220.     if (!cfg.BoolFlags.debug) wildCard(DelFile, ALL_FILES, FALSE, "", FALSE);
  221.     }
  222.   else
  223.     {
  224.     splitF(netLog,"Error:  Could not set current directory and delete temp files\n");
  225.     splitF(netLog,"Temporary dir:%s\n",temp2);
  226.     splitF(netLog,"  Current dir:%s\n",temp);
  227.     }
  228.   homeSpace();
  229.   }
  230.  
  231.  
  232.  
  233. /*
  234. * MakeTempDir()
  235. *
  236. * This function will create and move into a temporary directory.
  237. * This will always be off the homearea.
  238. */
  239. void MakeTempDir()
  240.   {
  241.   homeSpace();
  242.   MakeTempName();
  243.   if (mkdir(TDirBuffer) != 0) mPrintf("System error with mkdir!\n ");
  244.   chdir(TDirBuffer);
  245.   getcwd(TDirBuffer, 120);
  246.  
  247.   }
  248. /*
  249. * MakeTempName()
  250. *
  251. * This function gets a temporary name.
  252. */
  253. static void MakeTempName()
  254.   {
  255.   static char *seed = "tmp_%04d";
  256.   int i = 0;
  257.   sPrintf(TDirBuffer, seed, i++);
  258.   while (access(TDirBuffer, 0) != -1) sPrintf(TDirBuffer, seed, i++);
  259.   }
  260.  
  261. #define BAT_FILE  "%s\nif warn \n echo \"1\" >%s\nelse\n echo \"0\" >%s\nendif\n"
  262.  
  263. /*
  264. * FileIntegrity()
  265. *
  266. * This function does a file integrity check.
  267. * Note: we don't use spawn() since it adds 3K to the .EXE.
  268. */
  269. char FileIntegrity(char *filename)
  270.   {
  271.   char *c, *d, check[90], bad, NameUsed = FALSE;
  272.   int format;
  273.   FILE *fd;
  274.   if ((c = strchr(filename, '.')) != NULL)
  275.       {
  276.     if ((format = CompressType(filename)) == -1) return TRUE;
  277.     if (format > COMP_MAX - 1)                   return TRUE;
  278.     if (DeComp[format].IntWork == NULL)          return TRUE;
  279.     if (!getYesNo("INTEGR"))return TRUE;
  280.     Output_Citadel_Message("ONEMOM",NULL, NULL, NULL);
  281.     MakeTempName();
  282.     strCat(TDirBuffer, ".bat");
  283.     if (cfg.BoolFlags.debug) splitF(NULL,"File Open:%s\n",TDirBuffer);
  284.     fd = fopen(TDirBuffer, "w");
  285.     sprintf(check,"Execute %s",TDirBuffer);
  286.     MakeTempName();
  287.     /*
  288.     * Manually copy.  If we encounter "%g" then replace it with the
  289.     * name of the file to test.  If we never encounter it, then
  290.     * remember to append it to the end of the string after the copy
  291.     * has finished (NameUsed).
  292.     */
  293.     for (c = DeComp[format].IntWork, d = msgBuf.mbtext; *c; c++)
  294.         {
  295.       if (*c == '%' && *(c + 1) == 'g')
  296.           {
  297.         strCpy(d, filename);
  298.         NameUsed = TRUE;
  299.         while (*d) d++;
  300.         c++;      /* this will get us over the %g. */
  301.  
  302.         }
  303.       else *d++ = *c;
  304.  
  305.       }
  306.     *d++ = ' ';   /* harmless space padding. */
  307.     *d = 0;
  308.     if (!NameUsed) strCat(msgBuf.mbtext, filename);
  309.     fprintf(fd, BAT_FILE, msgBuf.mbtext, TDirBuffer, TDirBuffer);
  310.     fclose(fd);
  311.     Jsystem(check);
  312.     unlink(check);
  313.     if (cfg.BoolFlags.debug) splitF(NULL,"File Open:%s\n",TDirBuffer);
  314.     bad = TRUE;
  315.     if ((fd = fopen(TDirBuffer, "r")) != NULL)
  316.       {
  317.       if (fgets(check, sizeof check, fd) != NULL) bad = *check != '0';
  318.       fclose(fd);
  319.       unlink(TDirBuffer);
  320.       };
  321.     if (bad) return (char)!getYesNo("FAILIC");
  322.     else Output_Citadel_Message("PASSED",NULL, NULL, NULL);
  323.  
  324.     }
  325.   return TRUE;
  326.  
  327.   }
  328. /*
  329. * CompAvailable()
  330. *
  331. * This function determines if the given compression type has a compression
  332. * method defined.
  333. */
  334. char CompAvailable(char CompType)
  335.   {
  336.   return (char)(DeComp[CompType - 1].CompWork != NULL);
  337.  
  338.   }
  339. /*
  340. * NetDeCompress()
  341. *
  342. * This decompresses files for network dissection.
  343. */
  344. void NetDeCompress(char CompType, SYS_FILE fn)
  345.   {
  346.   char *c;
  347.   SYS_FILE dir;
  348.   char pwd[100];
  349.   if(  netDebug )splitF(netLog, "ComptType:%d File:%s\n", (int)CompType, fn);
  350.   if (DeComp[CompType - 1].DeWork == NULL)
  351.     {
  352.     splitF(netLog," No method for decompression, NetDeCompress aborted\n");
  353.     return;
  354.     };
  355.   strCpy(dir, fn);
  356.   c = &dir[strlen(dir) ];  /* get the directory path */
  357.   while(  *c != '/'  && *c != ':' )
  358.     {
  359.     c--;    /* move to the ':' or '/' */
  360.     };
  361.   c++;
  362.   *c = '\0';               /* terminate the directory spec */
  363.   c  = &fn[strlen(dir)];   /* get filename alone */
  364.   chdir(dir);              /* set the default directory    */
  365.   getcwd(pwd, 100);        /* get the absolute path */
  366.   if(  netDebug )splitF(netLog, "Compressed file dir:%s\n", pwd);
  367.   sPrintf(lbyte(pwd), pwd[strlen(pwd) - 1] == '/' ? "%s" : "/%s", c);
  368.   sprintf(cmdbuf,"%s \"%s\"", DeComp[CompType - 1].DeWork, pwd);
  369.   Jsystem(cmdbuf);         /* and unpack the archive */
  370.   }
  371. /*
  372. * Compress()
  373. *
  374. * This compresses the given files into the given archive file using the
  375. * given file.
  376. */
  377. void Compress(char CompType, char *Files, char *ArcFileName)
  378.   {
  379.   if (!CompAvailable(CompType)) return;
  380.   sprintf(cmdbuf, "%s %s %s", DeComp[CompType - 1].CompWork, ArcFileName, Files);
  381.   Jsystem(cmdbuf);
  382.  
  383.   }
  384. /*
  385. * GetUserCompression()
  386. *
  387. * This function handles a request for a compression protocol.  This can be
  388. * called either for a sysop (when selecting for Mass Transfers) or by a user
  389. * (eventually) for .RC.
  390. *
  391. * This returns the protocol selected (LHA_COMP, etc) or NO_COMP if none
  392. * was selected or if none are available.
  393. */
  394. int GetUserCompression()
  395.   {
  396.   int  rover, count = 0, x;
  397.   MenuId   id;
  398.   char *CompOpts[] =
  399.       {
  400.     " ", " ", " ", " ", ""
  401.     };
  402.     for (rover = 0; rover < NumElems(DeComp); rover++) {
  403.     if (DeComp[rover].CompWork != NULL) {
  404.     count++;
  405.     ExtraOption(CompOpts, DeComp[rover].MenuEntry);
  406.     if (whichIO == MODEM) mPrintf("%s\n ", DeComp[rover].MenuEntry);
  407.     }
  408.     }
  409.     if (count == 0) {
  410.     SysopError(NO_MENU, "No Compression methods");
  411.     return NO_COMP;
  412.     }
  413.     id = RegisterSysopMenu("Compress.mnu", CompOpts, " Compression ");
  414.     SysopMenuPrompt(id, "\n Compression method: ");
  415.     x = GetSysopMenuChar(id);
  416.     CloseSysopMenu(id);
  417.     switch (x) {
  418.     case 'P': return LZH_COMP;
  419.     case 'Z': return ZOO_COMP;
  420.     case 'L': return LHA_COMP;
  421.     case 'A': return ARC_COMP;
  422.     default:  return NO_COMP;
  423.     }
  424.     }
  425.